# -*- Mode: Python -*- vi:si:et:sw=4:sts=4:ts=4:syntax=python

import shutil
from pathlib import PurePath, Path

class Recipe(recipe.Recipe):
    name = 'meson'
    version = '0.55.3'
    licenses = [License.Apachev2]
    btype = BuildType.CUSTOM
    stype = SourceType.TARBALL
    url = 'https://github.com/mesonbuild/meson/releases/download/%(version)s/meson-%(version)s.tar.gz'
    tarball_checksum = '6bed2a25a128bbabe97cf40f63165ebe800e4fcb46db8ab7ef5c2b5789f092a5'
    patches = [
        # Remove on bump to 0.56, https://github.com/mesonbuild/meson/pull/7021
        'meson/0001-meson-Allow-cross-compile-without-a-native-compiler.patch',
    ]

    deps = ['ninja']

    files_bin = ['bin/meson']
    files_python = []

    async def install(self):
        # setup.py barfs if using posix paths on Windows
        if self.config.platform == Platform.WINDOWS:
            prefix = str(PurePath(self.config.prefix))
        else:
            prefix = self.config.prefix
        await shell.async_call([self.config.python_exe, 'setup.py', 'install', '--prefix', prefix],
                       cmd_dir=self.build_dir, env=self.env, logfile=self.logfile)
        if self.config.platform == Platform.WINDOWS:
            prefix = Path(self.config.prefix)
            for f in ('meson.exe', 'meson-script.py'):
                (prefix / 'Scripts' / f).replace(prefix / 'bin' / f)
            shutil.rmtree(prefix / 'Scripts')
